package ocean.chinacraft.common.world.item.armor;

import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import ocean.chinacraft.common.config.ApplicationConfig;
import ocean.chinacraft.common.util.ModUtil;
import ocean.chinacraft.common.world.item.armor.base.MaterialEnums;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

/**
 * 夜行衣
 *
 * @author zhang
 * @since 2021/12/5
 */
public class SilkNightArmor extends ArmorItem {

    public SilkNightArmor(MaterialEnums material, ArmorItem.Type type, Properties properties) {
        super(material, type, properties);
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(@NotNull ItemStack itemStack, @Nullable Level level, List<Component> textComponents,
                                @NotNull TooltipFlag tipFlag) {
        textComponents.add(Component.translatable("tooltip.night.nightarmor.0"));
        textComponents.add(Component.translatable("tooltip.night.nightarmor.1"));
        textComponents.add(Component.translatable("tooltip.night.nightarmor.2"));
        textComponents.add(Component.translatable("tooltip.night.nightarmor.3"));
    }

    @Override
    public boolean isDamageable(ItemStack stack) {
        return !ApplicationConfig.ENABLE_INFINITE_DURABILITY.get();
    }

    @Override
    public boolean hurtEnemy(ItemStack stack, LivingEntity entity, LivingEntity onBorken) {
        if (ApplicationConfig.ENABLE_INFINITE_DURABILITY.get()) {
            return false;
        }
        return super.hurtEnemy(stack, entity, onBorken);
    }

    @Override
    public int getDamage(ItemStack stack) {
        return ApplicationConfig.ENABLE_INFINITE_DURABILITY.get() ? 0 : super.getDamage(stack);
    }

    @Override
    public void setDamage(ItemStack stack, int damage) {
        if (!ApplicationConfig.ENABLE_INFINITE_DURABILITY.get()) {
            super.setDamage(stack, damage);
        }
    }

    public static boolean hasFood(Player player) {
        Integer food = ApplicationConfig.NEED_FOOD.get();
        return player.getFoodData().getFoodLevel() >= Objects.requireNonNullElse(food, 0);
    }

    public static boolean invalid(Player player) {
        return ModUtil.hasCorrectArmorOn(MaterialEnums.SILK_NIGHT_ARMOR, player) && hasFood(player) && player.isShiftKeyDown();
    }
}
